package com.qyhl.webtv.commonlib.utils.view.gridviewpager;

import android.content.Context;
import androidx.viewpager.widget.ViewPager;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Cookie on 2017/1/16.
 */

public class GridViewPager extends ViewPager {
    private List<GridView> mLists = new ArrayList<>();

    private GridViewPagerAdapter adapter;

    private ViewGroup parent;

    private List listAll;

    private int rowInOnePage;

    private int columnInOnePage;

    private int pageCount;

    private GridViewPagerDataAdapter gridViewPagerDataAdapter;

    public int getPageCount() {
        return pageCount;
    }

    public void setNestParent(ViewGroup parent) {
        this.parent = parent;
    }

    public GridViewPager(Context context) {

        super(context);

    }


    public GridViewPager(Context context, AttributeSet attrs) {

        super(context, attrs);

    }


    public GridViewPagerDataAdapter getGridViewPagerDataAdapter() {

        return gridViewPagerDataAdapter;

    }


    public void setGridViewPagerDataAdapter(GridViewPagerDataAdapter gridViewPagerDataAdapter) {

        this.gridViewPagerDataAdapter = gridViewPagerDataAdapter;

        if (gridViewPagerDataAdapter.listAll == null || gridViewPagerDataAdapter.listAll.size() == 0) {
            return;
        }

        listAll = gridViewPagerDataAdapter.listAll;

        rowInOnePage = gridViewPagerDataAdapter.rowInOnePage;

        columnInOnePage = gridViewPagerDataAdapter.columnInOnePage;

        init();

    }


    public void init() {

        int sizeInOnePage = rowInOnePage * columnInOnePage;

        pageCount = listAll.size() / sizeInOnePage;

        pageCount += listAll.size() % sizeInOnePage == 0 ? 0 : 1;

        if (mLists.size() > pageCount) {

            for (int i = mLists.size() - 1; i >= pageCount; i--) {

                mLists.remove(i);

            }

        }

        WrapContentGridView gv;

        int end;

        for (int i = 0; i < pageCount; i++) {

            final int pageIndex = i;

            if (i < mLists.size()) {

                gv = (WrapContentGridView) mLists.get(i);

            } else {

                gv = new WrapContentGridView(getContext());

                gv.setGravity(Gravity.CENTER);

                gv.setClickable(true);

                gv.setFocusable(true);

                mLists.add(gv);

            }

            gv.setNumColumns(columnInOnePage);

            end = Math.min((i + 1) * sizeInOnePage, listAll.size());

            gv.setAdapter(gridViewPagerDataAdapter.getGridViewAdapter(listAll.subList(i * sizeInOnePage, end), i));

            gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override

                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

                    gridViewPagerDataAdapter.onItemClick(arg0, arg1, arg2, arg3, pageIndex);

                }

            });

        }

        adapter = new GridViewPagerAdapter(getContext(), mLists);

        setAdapter(adapter);

    }


    @Override

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int height = 0;

        for (int i = 0; mLists != null && i < mLists.size(); i++) {

            View child = mLists.get(i);

            child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

            int h = child.getMeasuredHeight();

            if (h > height)

            {
                height = h;
            }

        }

        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height + getPaddingBottom() + getPaddingTop(), MeasureSpec.EXACTLY);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (parent != null) {
            getParent().requestDisallowInterceptTouchEvent(true);
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (parent != null) {
            getParent().requestDisallowInterceptTouchEvent(true);
        }
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (parent != null) {
            getParent().requestDisallowInterceptTouchEvent(true);
        }
        return super.onTouchEvent(ev);
    }

}
